package com.ljf.daily.consumptiondetail.api.service.impl;

import com.ljf.common.api.service.impl.BasisServiceImpl;
import com.ljf.common.exception.ServiceException;
import com.ljf.common.result.ServiceResultModel;
import com.ljf.common.utils.ServiceResultUtil;
import com.ljf.daily.consumptiondetail.domain.repository.DailyDatConsumptionDetailRepository;
import com.ljf.daily.consumptiondetail.domain.service.DailyDatConsumptionDetailDomainService;
import com.ljf.daily.consumptiondetail.domain.validator.DailyDatConsumptionDetailValidator;
import com.ljf.daily.consumptiondetail.dto.command.DailyDatConsumptionDetailCommand;
import com.ljf.daily.consumptiondetail.dto.command.DailyDatConsumptionDetailGenerateDataCommand;
import com.ljf.daily.consumptiondetail.dto.query.DailyDatConsumptionDetailQuery;
import com.ljf.daily.consumptiondetail.model.DailyDatConsumptionDetail;
import com.ljf.daily.consumptiondetail.service.IDailyDatConsumptionDetailService;
import com.ljf.daily.consumptiondetail.vo.DailyDatConsumptionDetailEditDataVO;
import com.ljf.daily.consumptiondetail.vo.DailyDatConsumptionDetailVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 日常消费明细  服务接口实现
 *
 * @author LJF
 * @since 2024-05-15
 */
@Service
public class DailyDatConsumptionDetailServiceImpl extends BasisServiceImpl<DailyDatConsumptionDetailDomainService, DailyDatConsumptionDetailValidator, DailyDatConsumptionDetailRepository, DailyDatConsumptionDetailQuery, DailyDatConsumptionDetailVO, DailyDatConsumptionDetail, DailyDatConsumptionDetailCommand> implements IDailyDatConsumptionDetailService {

    /**
     * 生成数据接口
     *
     * @param command
     * @return
     * @throws ServiceException
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ServiceResultModel generateData(DailyDatConsumptionDetailGenerateDataCommand command) throws ServiceException {
        validator.checkGenerateData(command);
        Integer num = domainService.generateData(command);
        return ServiceResultUtil.success(String.format("生成成功，已创建%s条记录", num));
    }

    /**
     * 获取更新数据
     *
     * @param date
     * @param dayConsumption
     * @return
     * @throws ServiceException
     */
    @Override
    public ServiceResultModel<DailyDatConsumptionDetailEditDataVO> editData(Date date, String dayConsumption) throws ServiceException {
        validator.checkEditData(date, dayConsumption);
        DailyDatConsumptionDetailEditDataVO result = domainService.editData(date, dayConsumption);
        List<DailyDatConsumptionDetailEditDataVO> resultList = new ArrayList<>();
        resultList.add(result);
        return ServiceResultUtil.success(resultList);
    }

}
